import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:video_player/video_player.dart';

import '../../../common/models/ly_broadcaster_model.dart';
import '../../../common/models/ly_video_model.dart';
import '../../../utils/ly_data_manager.dart';

class LYVideoHomeController extends GetxController {
  List<LyVideoModel> videoList = [];
  VideoPlayerController? playerController;
  final pageController = PageController();

  LyVideoModel? currentPayVideo;
  LyBroadcasterModel? currentUserModel;
  bool isPause = false;
  bool showLoading = false;

  void createPlayer(LyVideoModel model, {bool autoPlay = true}) async {
    isPause = false;
    if (playerController != null) {
      if (playerController?.value.isPlaying == true) {
        playerController?.pause();
      }
      playerController?.removeListener(() {});
      playerController?.dispose();
    }
    showLoading = true;
    update(['list']);
    currentPayVideo = model;
    final url = Uri.parse(model.mediaUrl ?? '');
    final controller = VideoPlayerController.networkUrl(url);
    controller.setLooping(true);
    await controller.initialize();
    if (autoPlay) {
      await controller.play();
    }
    playerController = controller;
    playerController?.addListener(() {
      if (isPause &&
          playerController != null &&
          playerController?.value.isPlaying == true) {
        playerController?.pause();
      }
      debugPrint("==========${playerController?.value}======================");
    });
    Future.delayed(const Duration(seconds: 3)).then((value) {
      showLoading = false;
      update(['list']);
    });
  }

  void refreshVideos() {
    videoList = LYDataManager.share.videosList
        .where((element) => element.isBlack != true)
        .toList();
  }

  void loadVideos() async {
    if (videoList.isNotEmpty) {
      return;
    }
    EasyLoading.show();
    await LYDataManager.share.initVideosData();
    videoList = LYDataManager.share.videosList
        .where((element) => element.isBlack != true)
        .toList();
    if (videoList.isNotEmpty) {
      currentPayVideo = videoList.first;
      final userId = currentPayVideo?.userId ?? '';
      LYDataManager.share.searchUser(userId).then((value) {
        currentUserModel = value;
        update(['list']);
      });
      createPlayer(videoList.first, autoPlay: true);
    }
    update(['list']);
    EasyLoading.dismiss();
  }

  void startPlay() {
    if (videoList.isEmpty) {
      return;
    }
    if (playerController != null) {
      isPause = false;
      update(['list']);
      playerController!.play();
    }
  }

  void stopPlay() {
    isPause = true;
    if (playerController != null) {
      playerController!.pause();
    }
    update(['list']);
  }

  @override
  void dispose() {
    if (playerController != null) {
      if (playerController?.value.isPlaying == true) {
        playerController?.pause();
      }
      playerController?.dispose();
    }
    super.dispose();
  }
}
